package com.jsbb.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 *
 * @author w
 * @date 2021-03-05
 */
public class DateUtils {

    private DateUtils() {
    }

    public static final String YM = "yyyy-MM";

    public static final String YMD_DATA = "yyyy-MM-dd";

    public static final String YMD01_DATA = "yyyy/MM/dd";

    public static final String YMDHMS_DATA = "yyyy-MM-dd HH:mm:ss";

    public static final String YMDSTRING_DATA = "yyyyMMddHHmmss";

    public static final String YMDHMSS = "yyyyMMddHHmmssSSS";

    public static final String YMDGB_DATA = "yyyy年MM月dd日";

    public static final String YMDTHMSGB_DATA = "yyyy年MM月dd日 HH时mm分ss秒";

    public static final String[] DAYNAMES = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    public static final String[] CONSTELLATION = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};

    public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};

    /**
     * 一天的毫秒数
     */
    public static final int ONE_DATE_MILLISECOND = 24 * 60 * 60 * 1000;

    /**
     * 当前年
     *
     * @return
     */
    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 当前月
     *
     * @return
     */
    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    /**
     * 当前日
     *
     * @return
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 当前周
     *
     * @return
     */
    public static int getWeek() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 当前星期
     *
     * @return
     */
    public static String getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return DAYNAMES[dayOfWeek - 1];
    }

    /**
     * 当前年月日
     *
     * @return
     */
    public static String getCurrentYmdCn() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DAY_OF_MONTH) + "日";
    }

    /**
     * Date转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) {
        date = date == null ? new Date() : date;
        String result = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            result = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 字符串转Date
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseDate(String dateStr, String format) {
        if (dateStr == null || dateStr.length() == 0) {
            return null;
        }
        Date date = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    /**
     * String转long
     *
     * @param date
     * @param format
     * @return
     */
    public static Long string2long(String date, String format) {
        date.replace("a", "b");
        try {
            if (StringUtils.hasLength(format)) {
                format = YMDHMS_DATA;
            }
            SimpleDateFormat sf = new SimpleDateFormat(format);
            return sf.parse(date).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * long转string
     *
     * @param time
     * @param format
     * @return
     */
    public static String long2String(long time, String format) {
        try {
            if (StringUtils.hasLength(format)) {
                format = YMDHMS_DATA;
            }
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date date = new Date(time);
            return sf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 两日期相差X天
     *
     * @param start
     * @param end
     * @return
     */
    public static int getDayDiff(Date start, Date end) {
        return (int) ((end.getTime() - start.getTime()) / ONE_DATE_MILLISECOND);
    }

    /**
     * 指定日期加X天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + days);
        return calendar.getTime();
    }

    /**
     * 指定日期加X月
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonth(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    /**
     * 指定日期加X年
     *
     * @param date
     * @param year
     * @return
     */
    public static Date addYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.add(Calendar.YEAR, year);
        return calendar.getTime();
    }

    /**
     * 指定日期加X毫秒数
     *
     * @param date
     * @param millisecond
     * @return
     */
    public static String addTime(Date date, long millisecond) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = "";
        try {
            //1分钟60秒，1秒1000毫秒
            str = sim.format(date.getTime() + millisecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 当天开始时间
     *
     * @param date
     * @return
     */
    public static Date dayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        return calendar.getTime();
    }

    /**
     * 当天结束时间
     *
     * @param date
     * @return
     */
    public static Date dayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 本月最后一天
     *
     * @return
     */
    public static Date getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 日期转星座
     *
     * @param date
     * @return
     */
    public static String date2Constellation(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (day < CONSTELLATION_EDGE_DAY[month]) {
                month = month - 1;
            }
            if (month >= 0) {
                return CONSTELLATION[month];
            }
            return CONSTELLATION[11];
        }
        return null;
    }
}
